(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			getGradeAttence();
//			initStudent();
		});
	});
	function getGradeAttence(){
		processBegin("数据加载中······");
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"queryAttendanceRegular",
				model:"attendance",
				userKey:userKey,
				data:{
					currentpage:1,
					pagesize:1000,
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data.data;
				if(list.length > 0){
					$("#rules-data").empty();
					$("#rules-data").html(_.template($("#rules-data-template").html(),list));
//					processEnd("加载成功");
					
					$("input[name='rule-checked']")[0].setAttribute("checked",true),
					initStudent();
					
					$("input[name='rule-checked']").unbind("change");
					$("input[name='rule-checked']").change(function(){
						processBegin("数据加载中······");
						initStudent();
					});
					
					$("#rules-data a").unbind("click");
					$("#rules-data a").click(function(){
						$("#detail-show").show();
						$("#right-tables").hide();
						var code = $(this).attr("code");
						$("#detail-show").attr("src","detail.html?code="+code);
					});
				}else{
					content += "没有查到相关结果";
					$("#rules-data").empty();
					$("#rules-data").append(content);
					processEnd("加载成功");
				}
			}else{
				content += "没有查到相关结果";
				$("#rules-data").empty();
				$("#rules-data").append(content);
				processError("加载失败！");
			}
		},null,null);	
	}
	function initStudent(){
		var userKey = $("#user-name",parent.parent.document).attr("key");		
		var classcode = $(".classes-choice-select",parent.document).attr("classcode");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var rolecode = $("input[name='rule-checked']:checked").val();
		var data = {
				cmd:"queryRoleStudentByClass",
				model:"roles",
				userKey:userKey,
				data:{
//					rolecode:rolecode,
					classcode:classcode,
					schoolcode:schoolcode,
				},
				remark:"",
				
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1){
				if(res.data != undefined && res.data != ""){
					var list = res.data;
					if(list.length > 0){
						$("#stu-data").empty();
						$("#stu-data").html(_.template($("#stu-data-template").html(),list));
						processEnd("加载成功");
					}else{
						content += "没有查到相关结果";
						$("#stu-data").empty();
						$("#stu-data").append(content);
						processEnd("加载成功");
					}
				}else{
					content += "没有查到相关结果";
					$("#stu-data").empty();
					$("#stu-data").append(content);
					processEnd("加载成功");
				}
			}else{
				content += "没有查到相关结果";
				$("#stu-data").empty();
				$("#stu-data").append(content);
				processError("加载失败！");
			}
		},null,null);	
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function pagebtns(){
		$(".classes-choice",parent.document).unbind("click");
		$(".classes-choice",parent.document).click(function(){
			$(".classes-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("classes-choice-select");
			processBegin("数据加载中······");
			initStudent();
		});	
		$("#save-stu-rule").click(function(){
			addStuRule();
		});
	}
	function addStuRule(){
		processBegin("绑定中······");
		var userKey = $("#user-name",parent.parent.document).attr("key");		
		var classcode = $(".classes-choice-select",parent.document).attr("classcode");
		var rolecode = $("input[name='rule-checked']:checked").val();
//		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var studentcode = "";
		$("input[name='stu-checked']:checked").each(function(){
			studentcode += $(this).val()+",";
		});
		studentcode = studentcode.substring(0,studentcode.length-1);
		var data = {
				cmd:"batchaddRole",
				model:"roles",
				userKey:userKey,
				data:{
					classcode:classcode,
					rolecode:rolecode,
					studentcode:studentcode
				},
				remark:"",
				
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("绑定成功");
				initStudent();
			}else{
				processError("绑定失败！");
			}
		},null,null);	
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();